using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
namespace Microsoft.Phone.Controls.Primitives
{
	[TemplatePart(Name = "SwitchTrack", Type = typeof(Grid)), TemplatePart(Name = "SwitchThumb", Type = typeof(FrameworkElement)), TemplatePart(Name = "SwitchRoot", Type = typeof(Grid)), TemplatePart(Name = "SwitchBackground", Type = typeof(UIElement)), TemplateVisualState(Name = "Normal", GroupName = "CommonStates"), TemplateVisualState(Name = "Dragging", GroupName = "CheckStates"), TemplateVisualState(Name = "Unchecked", GroupName = "CheckStates"), TemplateVisualState(Name = "Disabled", GroupName = "CommonStates"), TemplateVisualState(Name = "Checked", GroupName = "CheckStates")]
	public class ToggleSwitchButton : ToggleButton
	{
		private const string CommonStates = "CommonStates";
		private const string NormalState = "Normal";
		private const string DisabledState = "Disabled";
		private const string CheckStates = "CheckStates";
		private const string CheckedState = "Checked";
		private const string DraggingState = "Dragging";
		private const string UncheckedState = "Unchecked";
		private const string SwitchRootPart = "SwitchRoot";
		private const string SwitchBackgroundPart = "SwitchBackground";
		private const string SwitchTrackPart = "SwitchTrack";
		private const string SwitchThumbPart = "SwitchThumb";
		private const double _uncheckedTranslation = 0.0;
		public static readonly DependencyProperty SwitchForegroundProperty = DependencyProperty.Register("SwitchForeground", typeof(Brush), typeof(ToggleSwitchButton), new PropertyMetadata(null));
		private TranslateTransform _backgroundTranslation;
		private TranslateTransform _thumbTranslation;
		private Grid _root;
		private Grid _track;
		private FrameworkElement _thumb;
		private double _checkedTranslation;
		private double _dragTranslation;
		private bool _wasDragged;
		private bool _isDragging;
		public Brush SwitchForeground
		{
			get
			{
				return (Brush)base.GetValue(ToggleSwitchButton.SwitchForegroundProperty);
			}
			set
			{
				base.SetValue(ToggleSwitchButton.SwitchForegroundProperty, value);
			}
		}
		private double Translation
		{
			get
			{
				if (this._backgroundTranslation != null)
				{
					return this._backgroundTranslation.get_X();
				}
				return this._thumbTranslation.get_X();
			}
			set
			{
				if (this._backgroundTranslation != null)
				{
					this._backgroundTranslation.set_X(value);
				}
				if (this._thumbTranslation != null)
				{
					this._thumbTranslation.set_X(value);
				}
			}
		}
		public ToggleSwitchButton()
		{
			base.set_DefaultStyleKey(typeof(ToggleSwitchButton));
		}
		private void ChangeVisualState(bool useTransitions)
		{
			if (base.get_IsEnabled())
			{
				VisualStateManager.GoToState(this, "Normal", useTransitions);
			}
			else
			{
				VisualStateManager.GoToState(this, "Disabled", useTransitions);
			}
			if (this._isDragging)
			{
				VisualStateManager.GoToState(this, "Dragging", useTransitions);
				return;
			}
			bool? isChecked = base.get_IsChecked();
			if (isChecked.get_HasValue() && isChecked.GetValueOrDefault())
			{
				VisualStateManager.GoToState(this, "Checked", useTransitions);
				return;
			}
			VisualStateManager.GoToState(this, "Unchecked", useTransitions);
		}
		protected override void OnToggle()
		{
			bool? isChecked = base.get_IsChecked();
			base.set_IsChecked(new bool?(!isChecked.get_HasValue() || !isChecked.GetValueOrDefault()));
			this.ChangeVisualState(true);
		}
		public override void OnApplyTemplate()
		{
			if (this._track != null)
			{
				this._track.remove_SizeChanged(new SizeChangedEventHandler(this.OnSizeChanged));
			}
			if (this._thumb != null)
			{
				this._thumb.remove_SizeChanged(new SizeChangedEventHandler(this.OnSizeChanged));
			}
			if (this._root != null)
			{
				this._root.remove_ManipulationStarted(new EventHandler<ManipulationStartedEventArgs>(this.OnManipulationStarted));
				this._root.remove_ManipulationDelta(new EventHandler<ManipulationDeltaEventArgs>(this.OnManipulationDelta));
				this._root.remove_ManipulationCompleted(new EventHandler<ManipulationCompletedEventArgs>(this.OnManipulationCompleted));
			}
			base.OnApplyTemplate();
			this._root = (base.GetTemplateChild("SwitchRoot") as Grid);
			UIElement uIElement = base.GetTemplateChild("SwitchBackground") as UIElement;
			this._backgroundTranslation = ((uIElement == null) ? null : (uIElement.get_RenderTransform() as TranslateTransform));
			this._track = (base.GetTemplateChild("SwitchTrack") as Grid);
			this._thumb = (base.GetTemplateChild("SwitchThumb") as Border);
			this._thumbTranslation = ((this._thumb == null) ? null : (this._thumb.get_RenderTransform() as TranslateTransform));
			if (this._root != null && this._track != null && this._thumb != null && (this._backgroundTranslation != null || this._thumbTranslation != null))
			{
				this._root.add_ManipulationStarted(new EventHandler<ManipulationStartedEventArgs>(this.OnManipulationStarted));
				this._root.add_ManipulationDelta(new EventHandler<ManipulationDeltaEventArgs>(this.OnManipulationDelta));
				this._root.add_ManipulationCompleted(new EventHandler<ManipulationCompletedEventArgs>(this.OnManipulationCompleted));
				this._track.add_SizeChanged(new SizeChangedEventHandler(this.OnSizeChanged));
				this._thumb.add_SizeChanged(new SizeChangedEventHandler(this.OnSizeChanged));
			}
			this.ChangeVisualState(false);
		}
		private void OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
			e.set_Handled(true);
			this._isDragging = true;
			this._dragTranslation = this.Translation;
			this.ChangeVisualState(true);
			this.Translation = this._dragTranslation;
		}
		private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
		{
			e.set_Handled(true);
			double x = e.get_DeltaManipulation().get_Translation().get_X();
			Orientation orientation = (Math.Abs(x) >= Math.Abs(e.get_DeltaManipulation().get_Translation().get_Y())) ? 1 : 0;
			if (orientation == 1 && x != 0.0)
			{
				this._wasDragged = true;
				this._dragTranslation += x;
				this.Translation = Math.Max(0.0, Math.Min(this._checkedTranslation, this._dragTranslation));
			}
		}
		private void OnManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
			e.set_Handled(true);
			this._isDragging = false;
			bool flag = false;
			if (this._wasDragged)
			{
				bool? isChecked = base.get_IsChecked();
				double num = (isChecked.get_HasValue() && isChecked.GetValueOrDefault()) ? this._checkedTranslation : 0.0;
				if (this.Translation != num)
				{
					flag = true;
				}
			}
			else
			{
				flag = true;
			}
			if (flag)
			{
				this.OnClick();
			}
			this._wasDragged = false;
		}
		protected override void OnMouseLeave(MouseEventArgs e)
		{
		}
		private void OnSizeChanged(object sender, SizeChangedEventArgs e)
		{
			UIElement arg_40_0 = this._track;
			RectangleGeometry rectangleGeometry = new RectangleGeometry();
			rectangleGeometry.set_Rect(new Rect(0.0, 0.0, this._track.get_ActualWidth(), this._track.get_ActualHeight()));
			arg_40_0.set_Clip(rectangleGeometry);
			this._checkedTranslation = this._track.get_ActualWidth() - this._thumb.get_ActualWidth() - this._thumb.get_Margin().get_Left() - this._thumb.get_Margin().get_Right();
		}
	}
}
